package com.lebang.controller.api;

import com.alibaba.fastjson.JSONObject;
import com.lebang.feign.AppDictFeign;
import com.lebang.model.AppDictModel;
import com.lebang.model.RequestUserInfo;
import com.lebangweb.response.Callback;
import com.lebangweb.response.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author: ljx
 * @Date: 2020/12/15
 * @Version 1.0
 */
@RestController
@RequestMapping("/app/dict")
public class AppDictApi {
    @Autowired
    private AppDictFeign feign;

    /**
     * 根据key查询字典
     * @param key
     * @return
     */
    @GetMapping("/get")
    public JsonData get(HttpServletRequest request,@RequestParam("key") String key){
        RequestUserInfo userInfo=new RequestUserInfo(request);
        JsonData jsonData = feign.get(key, userInfo.getAppId());
        if (jsonData.isSuccess()){
            AppDictModel appDictModel = JSONObject.parseObject(JSONObject.toJSONString(jsonData.getData()), AppDictModel.class);
            return Callback.success("200","查询成功",appDictModel.getKeyvalue());
        }
        return Callback.fail("201","查询失败");
    }

}
